package com.xiaomi.mipushdemo;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.Application;
import android.content.Context;
import android.os.Process;
import android.util.Log;

import com.xiaomi.channel.commonutils.logger.LoggerInterface;
import com.xiaomi.mipush.sdk.Logger;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.xiaomi.mipush.sdk.MiPushClient.MiPushClientCallback;

import java.util.List;

/**
 * 这里的demo主要展示了两个功能： 1，打开日志。如静态代码块所示
 * 2，初始化push。因为push在Manifest中设置为运行在另外一个进程，所以本Application会被实例化两次。
 * ，只有非push进程才需要调用MiPushClient.init来初始化push。为了保证当应用进程被杀死后，push消息能够唤醒app并且顺利收到
 * ，务必在Application的onCreate中调用MiPushClient.init方法。
 * 
 * @author wuxiaojun
 */
public class DemoApplication extends Application {
    static {
        LoggerInterface newLogger = new LoggerInterface() {

            @Override
            public void setTag(String tag) {
                // ignore
            }

            @Override
            public void log(String content, Throwable t) {
                Log.d(TAG, content, t);
            }

            @Override
            public void log(String content) {
                Log.d(TAG, content);
            }
        };
        Logger.setLogger(newLogger);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        init();
    }

    // user your appid the key.
    public static final String APP_ID = "1000270";
    public static final String APP_KEY = "670100056270";

    public boolean init() {
        boolean shouldInit = true;
        ActivityManager am = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE));
        List<RunningServiceInfo> serviceInfos = am.getRunningServices(1000);
        for (RunningServiceInfo info : serviceInfos) {
            if (info.pid == Process.myPid()
                    && (getPackageName() + ":pushservice").equals(info.process)) {
                shouldInit = false;
            }
        }
        if (shouldInit) {
            CallbackImpl callback = new CallbackImpl();
            String category = callback.getCategory();
            callback.setCategory(null);
            MiPushClient.initialize(this, APP_ID, APP_KEY, callback);
            return true;
        }
        return false;
    }
    
    private static final String TAG = "com.xiaomi.mipushdemo";

    public class CallbackImpl extends MiPushClientCallback {

        public String getCategory() {
            return super.getCategory();
        }

        public void setCategory(String category) {
            super.setCategory(category);
        }

        @Override
        public void onUnsubscribeResult(long resultCode, String reason,
                String topic) {
            Log.d(TAG, "onUnsubscribeResult is called.");
        }

        @Override
        public void onSubscribeResult(long resultCode, String reason,
                String topic) {
            Log.d(TAG, "onSubscribeResult is called.");
        }

        @Override
        public void onReceiveMessage(String content, String alias, String topic) {
            Log.d(TAG, "onReceiveMessage is called. " + content + ", " + alias + ", " + topic);
        }

        @Override
        public void onInitializeResult(long resultCode, String reason,
                final String regID) {
            Log.d(TAG, "onInitializeResult is called. " + regID);
        }

        @Override
        public void onCommandResult(String command, long resultCode,
                String reason, List<String> params) {
            Log.d(TAG, "onCommandResult is called. " + command + ": " + params);
        }
    }
}
